﻿using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveSaleContestInfoBySaleContestIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveSaleContestInfoBySaleContestIdRequest, RetrieveSaleContestInfoBySaleContestIdResponse>
    {
        #region Properties
        public string Lang;
        public int SaleContestId;
        public UserRecord Dao;
        #endregion
        public RetrieveSaleContestInfoBySaleContestIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveSaleContestInfoBySaleContestIdRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.SaleContestId == 0)
            {
                throw new ArgumentException("Product ID must be set", "request");
            }
            else
            {
                SaleContestId = request.SaleContestId;
            }
            if (string.IsNullOrEmpty(request.Lang))
            {
                Lang = "en";
            }
            else
            {
                Lang = request.Lang;
            }
            if (request.CurrentUser == null || string.IsNullOrEmpty(request.CurrentUser.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveSaleContestInfoBySaleContestIdResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            SaleContestProgramRecord record = new SaleContestProgramRecord();
            SaleContestProgramInfo listProductItem = Providers.ProductHandbooks.RetrieveSaleContestInfoBySaleContestId(SaleContestId);
            Dictionary<int, SaleContestProgram> listSaleContest = Providers.ProductHandbooks.RetrieveAllSaleContestProgram(Dao).ToDictionary(x => x.SaleContestId);

            record = new SaleContestProgramRecord
            {
                SaleContestId = listProductItem.SaleContestId,
                SaleContestName = listProductItem.SaleContestName,
                Scope = listProductItem.Scope,
                GeneralInfo = listProductItem.GeneralInfo,
                SumaryBranch = listProductItem.SumaryBranch,
                SumaryMsbo = listProductItem.SumaryMsbo,
                SumaryRegion = listProductItem.SumaryRegion,
                HasData = listSaleContest.ContainsKey(listProductItem.SaleContestId) ? listSaleContest[listProductItem.SaleContestId].HasData : 0
            };

            response.Record = record;
            return isSuccess;
        }
    }
}
